<template>
	<a-form :ref="'ruleForm'" :model="formState" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
		<a-row :gutter="[10, 10]">
			<a-col :span="24">
				<a-radio-group
					v-model:value="formState.dataSetConfig.dataSetType"
					optionType="button"
					buttonStyle="solid"
					:options="[
						{ label: '静态', value: 0 },
						{ label: '数据源', value: 1 }
					]"
				>
				</a-radio-group>
			</a-col>
			<template v-if="formState.dataSetConfig.dataSetType == 0">
				<a-col :span="24">
					<a-form-item label="嵌入地址" name="title">
						<a-input v-model:value="formState.data.link.value" />
					</a-form-item>
				</a-col>
			</template>
			<template v-else>
				<a-col :span="24">
					<SelectTree @onChange="selectTreeChange" v-model:paramsJson="formState.dataSetConfig.dataSetJson" />
				</a-col>
				<a-col :span="24">
					<a-select
						style="width: 100%"
						placeholder="请选择数据源"
						v-model:value="formState.data.link.field"
						@change="selectDataSet"
						optionLabelProp="label"
					>
						<a-select-option v-for="optionItem in dataOption" :value="optionItem" :key="optionItem" :label="optionItem">
							{{ optionItem }}
						</a-select-option>
					</a-select>
					<!-- <a-input
						v-if="formState.dataSetConfig.dataSetId"
						class="mt-[10px]"
						v-model:value="formState.data.link.alias"
						placeholder="字段别名"
					>
						<template #addonBefore>
							<div class="field-prefix">
								<a-tag color="blue">{{ formState.dataSetConfig.dataSetId }}</a-tag>
							</div>
						</template>
					</a-input> -->
				</a-col>
			</template>
		</a-row>
	</a-form>
</template>

<script setup>
	import { ref, watch } from 'vue'
	import SelectTree from '@/views/plugin/dashboard/plugin/public/SelectTree/SelectTree.vue'
	import config from '../config'
	import datasetApi from '@/views/plugin/dashboard/api/datasetApi'
	import { designStore } from '@/views/plugin/dashboard/store'
	const useDesignStore = designStore()
	import { cloneDeep } from 'lodash-es'
	import { reactive, defineProps, defineEmits } from 'vue'
	const emits = defineEmits(['upDataConfig'])
	const props = defineProps({
		config: {
			type: Object,
			default: () => {
				return config
			}
		}
	})
	// 静态
	const staticData =
		props.config.dataSetConfig.dataSetType === 1 ? ref(cloneDeep(config.data)) : ref(cloneDeep(props.config.data))
	// 动态
	const dynamicData =
		props.config.dataSetConfig.dataSetType === 0 ? ref(cloneDeep(config.data)) : ref(cloneDeep(props.config.data))

	const formState = computed({
		get() {
			const dataSetConfig = props.config.dataSetConfig // 更新的状态
			// 初始值
			const dataSource = dataSetConfig.dataSetType === 1 ? dynamicData.value : staticData.value
			return {
				data: dataSource,
				dataSetConfig: dataSetConfig
			}
		},
		set(value) {
			if (value.dataSetConfig.dataSetType === 1) {
				dynamicData.value = value.data
			} else {
				staticData.value = value.data
			}
		}
	})

	// 数据源
	const dataSource = ref([])
	// 模拟接口返回下拉数据
	const dataOption = ref([])

	// 数据变化清空下拉
	const selectTreeChange = () => {
		formState.value.dataSetConfig.dataSetId = undefined
	}

	// 获取下拉数据
	watch(
		() => formState.value.dataSetConfig.dataSetJson,
		(newVal) => {
			if (props.config.dataSetConfig.dataSetType === 1) {
				const list = useDesignStore.dataSource || []
				let datasetId = ''
				let indicatorId = ''
				list.forEach((parent) => {
					parent.config.indicators.forEach((child) => {
						if (child.id === newVal.id) {
							datasetId = parent.id
							indicatorId = child.id
						}
					})
				})
				if (datasetId && indicatorId) {
					// 下拉数据
					datasetApi.querySelectedColumns({ datasetId, indicatorId }).then((res) => {
						dataOption.value = res || []
					})
					datasetApi.preview(newVal).then((res) => {
						dataSource.value = res || []
					})
				}
			}
		},
		{
			immediate: true,
			deep: true
		}
	)
	// 模拟接口返回下拉数据
	// const dataOption = ref([
	// 	{
	// 		name: {
	// 			name: 'weixin',
	// 			type: 'string',
	// 			desc: '微信'
	// 		},
	// 		order: 1
	// 	},
	// 	{
	// 		name: {
	// 			name: 'qqEmail',
	// 			type: 'string',
	// 			desc: 'qq邮箱'
	// 		},
	// 		order: 2
	// 	}
	// ])
	// 模拟接口返回数据源
	// const dataSource = [{ weixin: 'https://weixin.qq.com/', qqEmail: 'https://mail.qq.com/' }]
	// 选择数据
	const selectDataSet = (val) => {
		try {
			const value = dataSource.value[0][val]
			if (/http/.test(value)) {
				formState.value.data.link.value = value
			} else {
				formState.value.data.link.value = ''
			}
		} catch (error) {
			console.log(error, '数据格式错误')
			formState.value.data.link.value = ''
		}
	}

	// 更新组件内容
	watch(
		() => formState.value,
		(newVal) => {
			formState.value = newVal
			emits('upDataConfig', { ...props.config, ...newVal })
		},
		{
			deep: true
		}
	)
</script>
<style scoped>
	:deep(.ant-radio-button-wrapper) {
		padding: 0px;
		width: 100%;
	}

	:deep(.ant-radio-group) {
		display: flex;
		text-align: center;
	}
</style>
